<?php
/**
 * Created by PhpStorm.
 * User: tc-net modify by tom_xia
 * Date: 2018/3/14 0014
 * Time: 10:29
 * 统计接口 统计PU UV Share
 */
require_once dirname(__DIR__) . "/common.php";

use app\cls\tom\Queue;
use app\cls\tom\Response;
use app\cls\tom\SysResource;
use app\cls\tom\Tools;
use app\cls\tom\Visitor;

$host = @parse_url(isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : "", PHP_URL_HOST);
$visitor = new Visitor();
$visitorInfo = $visitor->getVisitorId();

$data['xm'] = isset($_POST['xm']) ? trim($_POST['xm']) : 'host';  //项目
$data['op'] = isset($_POST['op']) ? trim($_POST['op']) : 'pv';   //pv 还是share
$data['wz'] = isset($_POST['wz']) ? trim($_POST['wz']) : '';        //位置
$data['pt'] = isset($_POST['xm']) ? trim($_POST['xm']) : 'h5';    //平台 h5 web
$data['uniqueid'] = isset($_POST['uniqueid']) && $_POST["uniqueid"] != "undefined" ? trim($_POST['uniqueid']) : "";
$data['other'] = isset($_POST['other']) ? trim($_POST['other']) : '';
$data['uid'] = isset($_POST['vir_ID']) ? trim($_POST['vir_ID']) : $visitorInfo[1];
$data['ip'] = explode(",", Tools::get_client_ip())[0];
$data['id'] = isset($_POST['id']) ? $_POST['id'] : '';
$data['source'] = isset($_POST['source']) ? $_POST['source'] : '';
$data['time'] = date("Y-m-d H:i:s");

$suffix = rand(1, 5);
$key = REDIS_PREFIX . "test_PV_" . $suffix;//这是入统计队列的key
// $ugc_key=REDIS_PREFIX."test_ugc_statics_".$suffix;//这是入user_material表的分享统计
// $material_key=REDIS_PREFIX."test_material_statics_".$suffix;//这是入video表的分享统计

//开始入队列
if ($data['xm'] && $data['uid']) {
    $redis = SysResource::getRedis();

    //把统计数据入库
    $redis->lPush($key, json_encode($data));

    // //1.把ugc的分享入队列
    // if(!empty($data['uniqueid']) && !empty($data['id']) && $data['source']=="ugc" && $data['op']=="share"){
    //     Tools::writeLog(["detail"=>$data,"key"=>"UGC分享：".$ugc_key]);
    //     $redis->lPush($ugc_key,json_encode($data));
    // }
    // //2.把配音（material）的分享入队列
    // if(!empty($data['uniqueid']) && !empty($data['id']) && $data['source']=="material" && $data['op']=="share"){
    //     Tools::writeLog(["detail"=>$data,"key"=>"配音分享：".$material_key]);
    //     $redis->lPush($material_key,json_encode($data));
    // }

    Response::output(0, "ok", ["vir_ID" => $visitorInfo[1]]);
} else {
    Response::output(-1, "fail", ["vir_ID" => $visitorInfo[1]]);
}

